package com.avic.rong.utils;

import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.rong.R;


/**
 * 自定义toast,不重复显示
 *
 */
public class ToastSuccessUtil {

    private static Toast mToast;

    private static Handler mHandler = new Handler();
    private static Runnable r = new Runnable() {
        @Override
        public void run() {
            mToast.cancel();
            mToast = null;// toast隐藏后，将其置为null
        }
    };

    public static void showToast(String message) {
        LayoutInflater inflater = (LayoutInflater) Utils.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = null;// 自定义布局
        if (inflater != null) {
            view = inflater.inflate(R.layout.toast_addcar_success, null);
//            TextView text = view.findViewById(R.id.toast_message);// 显示的提示文字
//            text.setText(message);
        }

        mHandler.removeCallbacks(r);
        if (mToast == null) {// 只有mToast==null时才重新创建，否则只需更改提示文字
            mToast = new Toast(Utils.getContext());
            mToast.setDuration(Toast.LENGTH_SHORT);
            mToast.setGravity(Gravity.CENTER, 0, 100);
            mToast.setView(view);
        }
        mHandler.postDelayed(r, 2000);// 延迟2秒隐藏toast
        mToast.show();
    }
}
